<template>
  <div>
    <Header></Header>
    <div>
      <div class="empty" v-if="scenicList.length === 0">
        <p>空空如也，没有查询到您想搜索的景点~</p>
        <p>请换一个搜索词吧</p>
      </div>
      <div class="list" v-else>
        <ul>
          <!--  -->
          <li v-for="(item, index) in scenicList" :key="index">
            <div>
              <img :src="item.scenic_img" style="height: 40%" />
              <!-- <p>{{ item.line_journey }}</p> -->
              <p>{{ item.scenic_name }}</p>
              <div class="bottom">
                <span>￥{{ item.scenic_price }}起</span>
                <button
                  @click="
                    order(item)
                  "
                >
                  立即购买
                </button>
              </div>
            </div>
          </li>
        </ul>
      </div>
    </div>
    <Footer></Footer>
  </div>
</template>

<script>
import Header from "../components/MyHeader.vue";
import Footer from "../components/MyFooter.vue";
import { scenicOrder, selectScenic } from "@/api";
import moment from "moment";
export default {
  components: {
    Header,
    Footer,
  },
  data() {
    return {
      scenicList: [],
      name: "",
    };
  },
  mounted() {
    this.name = this.$route.params.name;
    this.getScenicList();
  },
  methods: {
    //user_id, scenic_id,total_price,ordered_time
    async order(data) {
      // this.$router.push({name:'order',query:{id:scenicId}})
      let userId = JSON.parse(sessionStorage.getItem("user")).user_id;
      let orderedTime = moment().format("YYYY-MM-DD HH:mm");
      // console.log(scenicId,price);
      let params = {
        user_id: userId,
        scenic_id: data.scenic_id,
        total_price: data.scenic_price,
        ordered_time: orderedTime,
        scenic_name: data.scenic_name,
      };
      console.log('下单参数',params);
      const res = await scenicOrder(params);
      // console.log('下单返回', res);
      if (res.data.code === 200) {
        this.$message.success("下单成功！详情在用户中心查看");
      }
    },
    async getScenicList() {
      const res = await selectScenic({ name: this.name });
      // console.log('名字模糊查询景点', res);
      this.scenicList = res.data.data;
      console.log(this.scenicList);
    },
  },
};
</script>

<style lang="scss" scoped>
.empty {
  color: blue;
  width: 290px;
  height: 300px;
  text-align: center;
  margin: 80px auto;
}
.list {
  padding: 30px;
}
.list ul {
  display: flex;
  flex-wrap: wrap;
  li {
    border: 1px solid #ddd;
    background-color: white;
    margin-left: 70px;
    margin-bottom: 30px;
    width: 280px;
    height: 350px;
    div {
      position: relative;
      padding: 10px;
      img {
        width: 100%;
        height: 60%;
      }
      p {
        margin: 15px 0;
        padding-left: 15px;
      }
      div {
        position: absolute;
        bottom: -60px;
        left: 30px;
        button {
          padding: 5px;
          color: #444;
          border-radius: 15px;
          color: white;
          line-height: 30px;
          background-color: rgb(0, 142, 237);
          margin-left: 100px;
        }
      }
    }
  }
}
</style>